//输液瓶签扫码计数器
var shuye_code_counter = 0;

$(function(){

    $(".yizhu_one_group").die().live("click",function(){
        if(document_type_global=="show_yizhu"){
            return;
        }
        var type = $(this).parent().find(".yizhu_type").val();
        var current_zhixing_state = $(this).parent().find(".zhixing_state").val();
		var chexiao_state = $(this).parent().find(".chexiao_state").val();
        var zuhao = $(this).attr("zuhao");
        var zhixing_opration = "";
        var next_zhixing_state = "";

        // 显示对话框
        showYizhuDialog(current_zhixing_state,'<table class="yizhu_one_group_tanchuang">'+$(this).html()+'</table>',chexiao_state);
        shuye_code_counter = 1;

        $(".zhixing_button").die().live("click",function(){
            zhixing_opration = $(this).val();
            if(zhixing_opration=="取消")
            {
                $("#yizhu_dialog").hide();
            }
			else if(zhixing_opration=="执行完毕")
            {
				updateYizhuZhixingState(patient_info_global.zhuyuan_id,type,zuhao,zhixing_opration,nurse_info_global.user_number);
				$("#yizhu_dialog").hide();
                var getHtmlValue = $(this).parent().parent().find(".yizhu_left:first-child").html();
				if( getHtmlValue.indexOf("测血压") > 0 || getHtmlValue.indexof("测血糖") > 0)
				{
					$.mobile.changePage(app_path+"page/tizheng/tizhengluru.html",{transition: "none", reverse:false},false);
				}
            }
            else
            {
                updateYizhuZhixingState(patient_info_global.zhuyuan_id,type,zuhao,zhixing_opration,nurse_info_global.user_number);
                $("#yizhu_dialog").hide();
            }

            // 置零
            shuye_code_counter = 0;
        });
    });
});

// 根据条件获取医嘱列表数据
function getYizhuList(zhuyuan_id,geiyao_fangshi,zhixing_state)
{

	setHeader();
	var today = arguments[3];
	var type = arguments[4];
	//alert(server_url+"Mobile/Yizhu/getYizhuListJson"+yiyuan_id+"/zhuyuan_id/"+zhuyuan_id+"/geiyao_fangshi/"+geiyao_fangshi+"/zhixing_state/"+zhixing_state+"/today/"+today+"/type/"+type+"/page/"+yizhu_list_page_global+"/user_number/"+getRequestOne('user_number'));
	var pinlv;
	$.ajax({
        type: "get",
        async: false,
        url:server_url+"Mobile/Yizhu/getYizhuListJson"+yiyuan_id+"/zhuyuan_id/"+zhuyuan_id+"/geiyao_fangshi/"+geiyao_fangshi+"/zhixing_state/"+zhixing_state+"/today/"+today+"/type/"+type+"/page/"+yizhu_list_page_global+"/user_number/"+getRequestOne('user_number'),
       // url:"http://192.168.2.101/zpdydhl/Mobile/Yizhu/getYizhuListJson/yiyuan_id/00/zhuyuan_id/370921/geiyao_fangshi/全部/zhixing_state/拒绝执行/today/false/type/长期/page/1/user_number/009",
		//url:"http://192.168.2.101:80/zpdydhl/Mobile/Yizhu/getYizhuListJson/yiyuan_id/00/zhuyuan_id/370921/geiyao_fangshi/全部/zhixing_state/拒绝执行/today/false/type/长期/page/1/user_number/009",
		//data: { zhuyuan_id:zhuyuan_id,geiyao_fangshi:geiyao_fangshi,zhixing_state:zhixing_state,today:today,type:type,page:yizhu_list_page_global,user_number:getRequestOne('user_number')},
        dataType: "jsonp",
        jsonp: "callbackGetYizhuListJson",
        jsonpCallback:"feedBackStateGetYizhuListJson",
        error:function(mes)
        {
        },
        beforeSend: loading,
        success: function(result) {
            if(result.ret=="success")
            {

                var yizhu_list = result.data;

                var str_html = "";
                for(var i=0;i<yizhu_list.length;i++)
                {

					if(yizhu_list[i].zuhao == null)
					{
						yizhu_list[i].zuhao = "";
					}
					if(yizhu_list[i].content_show == null)
					{
						yizhu_list[i].content_show = "";
					}
					if(yizhu_list[i].ciliang == null)
					{
						yizhu_list[i].ciliang = "";
					}
					if(yizhu_list[i].shiyong_danwei == null)
					{
						yizhu_list[i].shiyong_danwei = "";
					}
					if(yizhu_list[i].beizhu == null)
					{
						yizhu_list[i].beizhu = "";
					}
					if(yizhu_list[i].pinlv == null)
					{
						yizhu_list[i].pinlv = "";
					}
					if(yizhu_list[i].yongfa == null)
					{
						yizhu_list[i].yongfa = "";
					}
					if(yizhu_list[i].xiada_time == null)
					{
						yizhu_list[i].xiada_time = "";
					}
					if(yizhu_list[i].start_time == null)
					{
						yizhu_list[i].start_time = "";
					}
					if(yizhu_list[i].peiyao_info == null)
					{
						yizhu_list[i].peiyao_info = "";
					}
					if(yizhu_list[i].zhixing_info == null)
					{
						yizhu_list[i].zhixing_info = "";
					}
					if(yizhu_list[i].tingyao_info == null)
					{
						yizhu_list[i].tingyao_info = "";
					}

                    if(yizhu_list[i].islast=="alone"||yizhu_list[i].islast=="first")
                    {
						str_html += '<div class="yizhu_list_content" style="">';
						if (zhixing_state=="未配液")
							str_html += '<table class="yizhu_one_group_tanchuang" zuhao="'+yizhu_list[i].zuhao+'">';
						else
							str_html += '<table class="yizhu_one_group" zuhao="'+yizhu_list[i].zuhao+'">';
                    }
                    str_html += '<tr class="yizhu_one">'+
                                    '<td class="yizhu_left">'+yizhu_list[i].content_show+'</td>'+
                                    '<td class="yizhu_right">'+yizhu_list[i].ciliang+yizhu_list[i].shiyong_danwei+'</td>'+
                                '</tr>';
                    if(yizhu_list[i].islast=="alone"||yizhu_list[i].islast=="last")
                    {
						if(yizhu_list[i].yongfa_type == "诊疗项目" && yizhu_list[i].beizhu != null)
						{
							str_html += '<tr class="yizhu_one_group_yongfa">'+
											'<td class="yizhu_left">'+yizhu_list[i].beizhu+'</td>'+
											'<td class="yizhu_right">'+yizhu_list[i].pinlv+'</td>'+
										'</tr>'+
										'<tr class="yizhu_one_group_time">';
						}
						else
						{
							str_html += '<tr class="yizhu_one_group_yongfa">'+
											'<td class="yizhu_left">'+yizhu_list[i].yongfa+'</td>'+
											'<td class="yizhu_right">'+yizhu_list[i].pinlv+'</td>'+
										'</tr>'+
										'<tr class="yizhu_one_group_time">';
						}
						if (typeof(yizhu_list[i].start_time)=="undefined")
							str_html +='<td colspan="2">'+yizhu_list[i].xiada_time+'</td>';
						else
							str_html +='<td colspan="2">'+yizhu_list[i].start_time+'</td>';
                    	str_html +='</tr>';
						str_html+='<tr><td colspan="2">'
						if(zhixing_state=='待配液'||zhixing_state=='已校对'||zhixing_state=='未配液')
						{
							//计算总共剩余执行次数
							total_remained_number = yizhu_list[i].total_need_number - yizhu_list[i].total_finish_number;
							//如果总共剩余次数大于日执行次数，显示总剩余次数
							if(total_remained_number>yizhu_list[i].daily_need_number)
							{
								for(var k=0;k<total_remained_number;k++)
								{
									str_html+='<span class="red_block"></span>';
								}
							}
							else
							{
								for(var k=0;k<yizhu_list[i].daily_need_number;k++)
								{
									if (k<yizhu_list[i].daily_finish_number)
										str_html+='<span class="green_block"></span>';
									else
										str_html+='<span class="red_block"></span>';
								}
							}

							if (yizhu_list[i].last_time!=null && yizhu_list[i].last_time!='')
								str_html+='<span class="yizhu_one_group_time" style="float:right;">最近:'+yizhu_list[i].last_time+'</span>';
						}
						str_html+='</td></tr>';
						if (yizhu_list[i].peiyao_info!='false')
						{
							str_html +='<tr><td colspan="2"><font color="" style="color:#979797;font-weight:normal">'+yizhu_list[i].peiyao_info+'</font></td></tr>';
						}
						if (yizhu_list[i].jiaodui_info!='false')
						{
							str_html +='<tr><td colspan="2"><font color="" style="color:#979797;font-weight:normal">'+yizhu_list[i].jiaodui_info+'</font></td></tr>';
						}
						if (yizhu_list[i].zhixing_info!='false')
						{
							str_html +='<tr><td colspan="2"><font color="" style="color:#979797;font-weight:normal">'+yizhu_list[i].zhixing_info+'</font></td></tr>';
						}
						if (yizhu_list[i].tingyao_info!='false')
						{
							str_html +='<tr><td colspan="2"><font color="" style="color:#979797;font-weight:normal">'+yizhu_list[i].tingyao_info+'</font></td></tr>';
						}

                        str_html += '</table>'+
                                        '<hr/>';

                        if(today == false){
                            str_html += '<input type="hidden" class="yizhu_type" value="'+yizhu_list[i].type+'"/>'+
                                    '<input type="hidden" class="zhixing_state" value="'+yizhu_list[i].state+'"/>'+
									'<input type="hidden" class="chexiao_state" value="'+yizhu_list[i].chexiao_state+'"/>';

                        }else{
                            str_html += '<input type="hidden" class="yizhu_type" value="'+yizhu_list[i].type+'"/>'+
                                    '<input type="hidden" class="zhixing_state" value="'+yizhu_list[i].zhixing_state+'"/>'+
									'<input type="hidden" class="chexiao_state" value="'+yizhu_list[i].chexiao_state+'"/>';
                        }
						str_html += '</div>';
                    }
                }

				if(yizhu_list_page_global>1 && zhixing_state=='执行完毕')
					$(".yizhu_list_layout").append(str_html);
				else
					$(".yizhu_list_content_parent").html(str_html);
				if (zhixing_state=='执行完毕' && result.yehsu>1 && yizhu_list_page_global < result.yehsu)
					$(".jiazai_gengdong").show();
				else
				{
					yizhu_list_page_global=1;
					$('.jiazai_gengdong').hide();
				}
				/*加载loding消失*/
				$('#m').remove();
			    $('#lo').remove();
			    /*end*/
            }
            else
            {
                alert("获取医嘱信息失败，请稍后再重新尝试。");
            }
        },
    });
}
//加载更多医嘱
$(".jiazai_gengdong").die().live('click',function(){
	yizhu_list_page_global = yizhu_list_page_global+1;
	getYizhuList(patient_info_global.zhuyuan_id,"全部","执行完毕");
})

//根据日期查询历史医嘱执行时间
function getLishiYizhuList(zhuyuan_id,geiyao_fangshi,data)
{
	setHeader();
	$.ajax({
        type: "get",
        async: false,
        url:server_url+"Mobile/Yizhu/getLishiYizhuListJson"+yiyuan_id,
        data: { zhuyuan_id : zhuyuan_id,geiyao_fangshi:geiyao_fangshi,data:data},
        dataType: "jsonp",
        jsonp: "callbackGetLishiYizhuListJson",
        jsonpCallback:"feedBackStateGetLishiYizhuListJson",
        error:function(mes)
        {
			alert("当前网络状况不佳，请稍后再重新进行此操作。");
        },
        success: function(result) {
            if(result.ret=="success")
            {
                var yizhu_list = result.data;
                var str_html = "";
                for(var i=0;i<yizhu_list.length;i++)
                {
					if(yizhu_list[i].zuhao == null)
					{
						yizhu_list[i].zuhao = "";
					}
					if(yizhu_list[i].content_show == null)
					{
						yizhu_list[i].content_show = "";
					}
					if(yizhu_list[i].ciliang == null)
					{
						yizhu_list[i].ciliang = "";
					}
					if(yizhu_list[i].shiyong_danwei == null)
					{
						yizhu_list[i].shiyong_danwei = "";
					}
					if(yizhu_list[i].beizhu == null)
					{
						yizhu_list[i].beizhu = "";
					}
					if(yizhu_list[i].pinlv == null)
					{
						yizhu_list[i].pinlv = "";
					}
					if(yizhu_list[i].yongfa == null)
					{
						yizhu_list[i].yongfa = "";
					}
					if(yizhu_list[i].xiada_time == null)
					{
						yizhu_list[i].xiada_time = "";
					}
					if(yizhu_list[i].start_time == null)
					{
						yizhu_list[i].start_time = "";
					}
					if(yizhu_list[i].peiyao_info == null)
					{
						yizhu_list[i].peiyao_info = "";
					}
					if(yizhu_list[i].zhixing_info == null)
					{
						yizhu_list[i].zhixing_info = "";
					}
					if(yizhu_list[i].tingyao_info == null)
					{
						yizhu_list[i].tingyao_info = "";
					}

                    if(yizhu_list[i].islast=="alone"||yizhu_list[i].islast=="first")
                    {
						str_html += '<div class="yizhu_list_content" style="">';
                        str_html += '<table class="yizhu_one_group" zuhao="'+yizhu_list[i].zuhao+'">';
                    }
                    str_html += '<tr class="yizhu_one">'+
                                    '<td class="yizhu_left">'+yizhu_list[i].content_show+'</td>'+
                                    '<td class="yizhu_right">'+yizhu_list[i].ciliang+yizhu_list[i].shiyong_danwei+'</td>'+
                                '</tr>';
                    if(yizhu_list[i].islast=="alone"||yizhu_list[i].islast=="last")
                    {
						if(yizhu_list[i].yongfa_type == "诊疗项目" && yizhu_list[i].beizhu != null)
						{
							str_html += '<tr class="yizhu_one_group_yongfa">'+
											'<td class="yizhu_left">'+yizhu_list[i].beizhu+'</td>'+
											'<td class="yizhu_right">'+yizhu_list[i].pinlv+'</td>'+
										'</tr>'+
										'<tr class="yizhu_one_group_time">';
						}
						else
						{
							str_html += '<tr class="yizhu_one_group_yongfa">'+
											'<td class="yizhu_left">'+yizhu_list[i].yongfa+'</td>'+
											'<td class="yizhu_right">'+yizhu_list[i].pinlv+'</td>'+
										'</tr>'+
										'<tr class="yizhu_one_group_time">';
						}

						if (typeof(yizhu_list[i].start_time)=="undefined")
							str_html +='<td colspan="2">'+yizhu_list[i].xiada_time+'</td>';
						else
							str_html +='<td colspan="2">'+yizhu_list[i].start_time+'</td>';
                    	str_html +='</tr>';

						if (yizhu_list[i].peiyao_info!='false')
						{
							str_html +='<tr><td colspan="2"><font color="" style="color:#979797;font-weight:normal">'+yizhu_list[i].peiyao_info+'</font></td></tr>';
						}
						if (yizhu_list[i].zhixing_info!='false')
						{
							str_html +='<tr><td colspan="2"><font color="" style="color:#979797;font-weight:normal">'+yizhu_list[i].zhixing_info+'</font></td></tr>';
						}
						if (yizhu_list[i].tingyao_info!='false')
						{
							str_html +='<tr><td colspan="2"><font color="" style="color:#979797;font-weight:normal">'+yizhu_list[i].tingyao_info+'</font></td></tr>';
						}

                        str_html += '</table>'+
                                        '<hr/>';
						str_html += '<input type="hidden" class="zhixing_state" value="执行完毕"/>';
						str_html += '</div>';
                    }
                }
				$(".yizhu_list_layout").html(str_html);
            }
            else
            {
                alert("获取医嘱信息失败，请稍后再重新尝试。");
            }
        }
    });
}

// 更新医嘱执行状态
function updateYizhuZhixingState(zhuyuan_id,type,zuhao,zhixing_opration,user_number)
{
	$.ajax({
		type: "get",
		async: false,
		url:server_url+"Mobile/Yizhu/updateYizhuZhixingStateJson"+yiyuan_id,
		data: { zhuyuan_id:zhuyuan_id,type:type,zuhao:zuhao,zhixing_opration:zhixing_opration,user_number:user_number},
		dataType: "jsonp",
		jsonp: "callbackUpdateYizhuZhixingStateJson",
		jsonpCallback:"feedBackStateUpdateYizhuZhixingStateJson",
		error:function(mes)
		{
		},
		success: function(result) {
		if(result.ret=="success")
		{
		$("[zuhao='"+zuhao+"']").next().remove();
		$("[zuhao='"+zuhao+"']").remove();
		setPageZhuangtai(zhixing_opration);
		}
		else
		{
		alert("失败");
		}
		}
	});
}

// 显示对话框
function showYizhuDialog(current_zhixing_state,content,chexiao_state)
{
    var str_button = "";
    if(current_zhixing_state=="待配液")
    {
        str_button = '<input class="zhixing_button" type="button" value="配液" />';
    }
    else if(current_zhixing_state=="已配液")
    {
        str_button = '<input class="zhixing_button" type="button" value="校对" />';
		if(chexiao_state == "可撤销")
		{
			str_button += '<input class="zhixing_button" type="button" value="撤销" />';
		}
    }
    else if(current_zhixing_state=="已校对")
    {
        str_button = '<input class="zhixing_button" type="button" value="开始执行" />';
		if(chexiao_state == "可撤销")
		{
			str_button += '<input class="zhixing_button" type="button" value="撤销" />';
		}
    }
    else if(current_zhixing_state=="开始执行")
    {
        str_button = '<input class="zhixing_button" type="button" value="暂停执行" />'+
                        '<input class="zhixing_button" type="button" value="执行完毕" />';
    }
    else if(current_zhixing_state=="暂停执行")
    {
        str_button = '<input class="zhixing_button" type="button" value="开始执行" />'+
                        '<input class="zhixing_button" type="button" value="执行完毕" />';
    }
    else if(current_zhixing_state=="执行完毕")
    {
        return;
    }

    str_button += '<input class="zhixing_button" id="yizhu_cancel" type="button" value="取消" />';

    $(".yizhu_dialog_title").html(current_zhixing_state);
    $(".yizhu_dialog_content").html(content);
    $(".yizhu_dialog_button").html(str_button);
    $("#yizhu_dialog").show();
}

// 扫描瓶签操作
function saomaShuyePingqian(code)
{
	var zhuyuan_id = patient_info_global.zhuyuan_id;
	var zuhao = code;
	var current_zhixing_state = "";
	var zhixing_opration = "";
	// 首先获取当前执行状态
	$.ajax({
		type: "get",
		async: false,
		url:server_url+"Mobile/Yizhu/getYizhuZhixingStateJson"+yiyuan_id,
		data: {zuhao:zuhao,zhuyuan_id:zhuyuan_id},
		dataType: "jsonp",
		jsonp: "callbackGetYizhuZhixingStateJson",
		jsonpCallback:"feedBackStateGetYizhuZhixingStateJson",
		error:function(mes)
		{
		},
		success: function(result) {
			if(result.ret=="success")
			{
				current_zhixing_state = result.data;
				yizhu_type = result.yizhu_type;
				//通过扫码次数计数，确认为第一次扫码
				if(shuye_code_counter==0)
				{
					var current_zhixing_state_yemian = '';
					var content = "";
					$(".yizhu_one_group").each(function(i,e){
						var temp_type = $(this).parent().find(".yizhu_type").val();
						var temp_zuhao = $(this).attr("zuhao");
						if(temp_zuhao==zuhao)
						{
							current_zhixing_state_yemian = $(this).parent().find(".zhixing_state").val();
							content = '<table class="yizhu_one_group_tanchuang">'+$(this).html()+'</table>';
						}
					});
					if(current_zhixing_state_yemian=="执行完毕")
					{
						return;
					}
					if(result.is_patient=="false" && document_type_global == "yizhuZhixing" )
					{
						alert("该组药品不属于当前患者");
						return;
					}

					if(content=="")
					{
						if(current_zhixing_state=="已校对")
						{
							alert("不属于"+$("a.ui-btn-active").html()+"的医嘱，请在已校对或未执行中查看");
						}
						else
						{
							alert("不属于"+$("a.ui-btn-active").html()+"的医嘱，请在"+current_zhixing_state+"中查看");
						}
						return;
					}
					else if(current_zhixing_state=="执行完毕")
					{
						alert("该项医嘱已执行完毕，无需重复执行！");
						return;
					}

					showYizhuDialog(current_zhixing_state,content,result.yongfa_type);
					shuye_code_counter = 1;
					$(".zhixing_button").die().live("click",function(){
						zhixing_opration = $(this).val();
						if(zhixing_opration=="取消")
						{
							$("#yizhu_dialog").hide();
						}
						else
						{
							updateYizhuZhixingState(zhuyuan_id,yizhu_type,zuhao,zhixing_opration,nurse_info_global.user_number);
							$("#yizhu_dialog").hide();
						}
						// 置零
						shuye_code_counter = 0;
					});
				}
				//通过扫码次数计数，确认为第二次扫码
				else
				{
					// 执行
					if(current_zhixing_state=="待配液")
					{
						zhixing_opration = "配液";
					}
					else if(current_zhixing_state=="已配液")
					{
						zhixing_opration = "校对";
					}
					else if(current_zhixing_state=="已校对")
					{
						zhixing_opration = "开始执行";
					}
					else if(current_zhixing_state=="开始执行")
					{
						zhixing_opration = "执行完毕";
					}
					else if(current_zhixing_state=="暂停执行")
					{
						zhixing_opration = "开始执行";
					}
					updateYizhuZhixingState(zhuyuan_id,yizhu_type,zuhao,zhixing_opration,nurse_info_global.user_number);
					$("#yizhu_dialog").hide();
					// 置零
					shuye_code_counter=0;
				}
			}
			else
			{
				alert(result.msg);
			}
		}
	});
}

//扫码完成后自动改变当前页面显示状态
function setPageZhuangtai(option)
{
	if(option=="配液")
	{
		$("#yipeiye a").addClass('ui-btn-active');
		$("#yipeiye a").css("color","#ffffff");
		$("#daipeiye a").removeClass('ui-btn-active');
		$("#daipeiye a").css("color","#0D85C0");
		f2_key_number=2;
		getYizhuList(patient_info_global.zhuyuan_id,"输液",'已配液');

	}
	else if(option=="校对")
	{
		$("#yijiaodui a").addClass('ui-btn-active');
		$("#yijiaodui a").css("color","#ffffff");
		$("#yipeiye a").removeClass('ui-btn-active');
		$("#yipeiye a").css("color","#0D85C0");
		f2_key_number=3;
		getYizhuList(patient_info_global.zhuyuan_id,"输液",'已校对');

	}
	else if(option=="开始执行")
	{

		$("#kaishizhixing a").addClass('ui-btn-active');
		$("#kaishizhixing a").css("color","#ffffff");
		$("#weizhixing a").removeClass('ui-btn-active');
		$("#weizhixing a").css("color","#0D85C0");
		$("#zantingzhixing a").removeClass('ui-btn-active');
		$("#zantingzhixing a").css("color","#0D85C0");
		getYizhuList(patient_info_global.zhuyuan_id,"全部",'开始执行');
		f2_key_number=2;
	}
	else if(option=="暂停执行")
	{

		$("#zantingzhixing a").addClass('ui-btn-active');
		$("#zantingzhixing a").css("color","#ffffff");
		$("#kaishizhixing a").removeClass('ui-btn-active');
		$("#kaishizhixing a").css("color","#0D85C0");
		getYizhuList(patient_info_global.zhuyuan_id,"全部",'暂停执行');
		f2_key_number=3;
	}
	else if(option=="执行完毕")
	{
		$("#zhixingwanbi a").addClass('ui-btn-active');
		$("#zhixingwanbi a").css("color","#ffffff");
		$("#kaishizhixing a").removeClass('ui-btn-active');
		$("#kaishizhixing a").css("color","#0D85C0");
		$("#zantingzhixing a").removeClass('ui-btn-active');
		$("#zantingzhixing a").css("color","#0D85C0");
		getYizhuList(patient_info_global.zhuyuan_id,"全部",'执行完毕');
		f2_key_number=4;
	}
}
